package com.datamation.swadeshi.control.data;

import java.util.ArrayList;

import com.datamation.swadeshi.R;
import com.datamation.swadeshi.model.InvDet;
import com.datamation.swadeshi.model.InvHed;
import com.datamation.swadeshi.model.OrdHed;
import com.datamation.swadeshi.model.mapping.LoadingMapper;
import com.datamation.swadeshi.model.mapping.VanSalesMapper;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class InvHedDS {

	private SQLiteDatabase dB;
	private DatabaseHelper dbHelper;
	Context context;
	private String TAG="swadeshi";
	
	public static final String SETTINGS = "SETTINGS";
	public static SharedPreferences localSP;
	
	public InvHedDS (Context context){
		this.context = context;
		dbHelper = new DatabaseHelper(context);
	}
	
	public void open() throws SQLException {
		dB = dbHelper.getWritableDatabase();
	}
	
	
	@SuppressWarnings("static-access")
	public int createOrUpdateInvHed(ArrayList<InvHed>  list) {
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		Cursor cursor = null;
		
	try{
		
	    for (InvHed invHed : list) {
	    	
				String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FINVHED 
					+ " WHERE " + dbHelper.FINVHED_REFNO + " = '" +invHed.getFINVHED_REFNO() + "'";	
			
				cursor = dB.rawQuery(selectQuery, null);
				
	    		ContentValues values = new ContentValues();
				
				values.put(dbHelper.FINVHED_REFNO, invHed.getFINVHED_REFNO());
				values.put(dbHelper.FINVHED_ADD_DATE, invHed.getFINVHED_ADD_DATE());
				values.put(dbHelper.FINVHED_ADD_MACH, invHed.getFINVHED_ADD_MACH());
				values.put(dbHelper.FINVHED_ADD_USER, invHed.getFINVHED_ADD_USER());
				values.put(dbHelper.FINVHED_APP_DATE, invHed.getFINVHED_APP_DATE());
				values.put(dbHelper.FINVHED_APPSTS, invHed.getFINVHED_APPSTS());
				values.put(dbHelper.FINVHED_APP_USER, invHed.getFINVHED_APP_USER());
				values.put(dbHelper.FINVHED_BP_TOTAL_DIS, invHed.getFINVHED_BP_TOTAL_DIS());
				values.put(dbHelper.FINVHED_B_TOTAL_AMT, invHed.getFINVHED_B_TOTAL_AMT());
				values.put(dbHelper.FINVHED_B_TOTAL_DIS, invHed.getFINVHED_B_TOTAL_DIS());
				values.put(dbHelper.FINVHED_B_TOTAL_TAX, invHed.getFINVHED_B_TOTAL_TAX());
				values.put(dbHelper.FINVHED_COST_CODE, invHed.getFINVHED_COST_CODE());
				values.put(dbHelper.FINVHED_CUR_CODE, invHed.getFINVHED_CUR_CODE());
				values.put(dbHelper.FINVHED_CUR_RATE, invHed.getFINVHED_CUR_RATE());
				values.put(dbHelper.FINVHED_DEB_CODE, invHed.getFINVHED_DEB_CODE());
				values.put(dbHelper.FINVHED_DIS_PER, invHed.getFINVHED_DIS_PER());
				values.put(dbHelper.FINVHED_START_TIME_SO, invHed.getFINVHED_START_TIME_SO());
				values.put(dbHelper.FINVHED_END_TIME_SO, invHed.getFINVHED_END_TIME_SO());
				values.put(dbHelper.FINVHED_LONGITUDE, invHed.getFINVHED_LONGITUDE());
				values.put(dbHelper.FINVHED_LATITUDE, invHed.getFINVHED_LATITUDE());
				values.put(dbHelper.FINVHED_LOC_CODE, invHed.getFINVHED_LOC_CODE());
				values.put(dbHelper.FINVHED_MANU_REF, invHed.getFINVHED_MANU_REF());
				values.put(dbHelper.FINVHED_RECORD_ID, invHed.getFINVHED_RECORD_ID());
				values.put(dbHelper.FINVHED_REMARKS, invHed.getFINVHED_REMARKS());
				values.put(dbHelper.FINVHED_REPCODE, invHed.getFINVHED_REPCODE());
				values.put(dbHelper.FINVHED_TAX_REG, invHed.getFINVHED_TAX_REG());
				values.put(dbHelper.FINVHED_TIMESTAMP_COLUMN, invHed.getFINVHED_TIMESTAMP_COLUMN());
				values.put(dbHelper.FINVHED_TOTAL_AMT, invHed.getFINVHED_TOTAL_AMT());
				values.put(dbHelper.FINVHED_TOTALDIS, invHed.getFINVHED_TOTALDIS());
				values.put(dbHelper.FINVHED_TOTAL_TAX, invHed.getFINVHED_TOTAL_TAX());
				values.put(dbHelper.FINVHED_TXN_TYPE, invHed.getFINVHED_TXN_TYPE());
				values.put(dbHelper.FINVHED_TXN_DATE, invHed.getFINVHED_TXN_DATE());
				values.put(dbHelper.FINVHED_ADDRESS, invHed.getFINVHED_ADDRESS());
				values.put(dbHelper.FINVHED_TOTAL_ITM_DIS, invHed.getFINVHED_TOTAL_ITM_DIS());
				values.put(dbHelper.FINVHED_TOT_MKR_AMT, invHed.getFINVHED_TOT_MKR_AMT());
				values.put(dbHelper.FINVHED_IS_SYNCED, "0");
				values.put(dbHelper.FINVHED_IS_ACTIVE, invHed.getFINVHED_IS_ACTIVE());
				values.put(dbHelper.FINVHED_ROUTE_CODE, invHed.getFINVHED_ROUTE_CODE());
				values.put(dbHelper.FINVHED_REFNO1, invHed.getFINVHED_REFNO1());
				values.put(dbHelper.FINVHED_HED_DIS_VAL, invHed.getFINVHED_HED_DIS_VAL());
				values.put(dbHelper.FINVHED_HED_DIS_PER_VAL, invHed.getFINVHED_HED_DIS_PER_VAL());
				values.put(dbHelper.FINVHED_PAYMENT_TYPE, invHed.getFINVHED_PAYMENT_TYPE());

				int cn = cursor.getCount();
				if(cn>0){
					
					count = dB.update(dbHelper.TABLE_FINVHED, values,dbHelper.FINVHED_REFNO + " =?",	
								new String[] { String.valueOf(invHed.getFINVHED_REFNO())});
				}else{
					
					count = (int) dB.insert(dbHelper.TABLE_FINVHED, null, values);
					
				}
				
				}
		}catch (Exception e) {
		
			Log.v(TAG+" Exception", e.toString());
	
		}finally {  
			if (cursor !=null) {
				cursor.close();
			}
			dB.close();
		}
		return count;
				
	}
	
	public ArrayList<InvHed> getAllActiveInvhed(String refno) {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<InvHed> list = new ArrayList<InvHed>();
		
		
		@SuppressWarnings("static-access")
		String selectQuery = "select * from "+dbHelper.TABLE_FINVHED
								+" Where "+dbHelper.FINVHED_IS_ACTIVE+"='1' and "+dbHelper.FINVHED_REFNO+"='"
								+refno+"' and "+dbHelper.FINVHED_IS_SYNCED+"='0'";
		
	
		Cursor cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){
			
			InvHed invHed =new InvHed();
		
			invHed.setFINVHED_ID(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_ID)));
			invHed.setFINVHED_REFNO(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_REFNO)));
			invHed.setFINVHED_ADD_DATE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_ADD_DATE)));
			invHed.setFINVHED_ADD_MACH(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_ADD_MACH)));
			invHed.setFINVHED_ADD_USER(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_ADD_USER)));
			invHed.setFINVHED_APP_DATE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_APP_DATE)));
			invHed.setFINVHED_APPSTS(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_APPSTS)));
			invHed.setFINVHED_APP_USER(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_APP_USER)));
			invHed.setFINVHED_BP_TOTAL_DIS(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_BP_TOTAL_DIS)));
			invHed.setFINVHED_B_TOTAL_AMT(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_B_TOTAL_AMT)));
			invHed.setFINVHED_B_TOTAL_DIS(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_B_TOTAL_DIS)));
			invHed.setFINVHED_B_TOTAL_TAX(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_B_TOTAL_TAX)));
			invHed.setFINVHED_COST_CODE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_COST_CODE)));
			invHed.setFINVHED_CUR_CODE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_CUR_CODE)));
			invHed.setFINVHED_CUR_RATE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_CUR_RATE)));
			invHed.setFINVHED_DEB_CODE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_DEB_CODE)));
			invHed.setFINVHED_DIS_PER(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_DIS_PER)));
			invHed.setFINVHED_START_TIME_SO(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_START_TIME_SO)));
			invHed.setFINVHED_END_TIME_SO(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_END_TIME_SO)));
			invHed.setFINVHED_LONGITUDE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_LONGITUDE)));
			invHed.setFINVHED_LATITUDE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_LATITUDE)));
			invHed.setFINVHED_LOC_CODE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_LOC_CODE)));
			invHed.setFINVHED_MANU_REF(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_MANU_REF)));
			invHed.setFINVHED_RECORD_ID(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_RECORD_ID)));
			invHed.setFINVHED_REMARKS(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_REMARKS)));
			invHed.setFINVHED_REPCODE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_REPCODE)));
			invHed.setFINVHED_TAX_REG(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_TAX_REG)));
			invHed.setFINVHED_TIMESTAMP_COLUMN(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_TIMESTAMP_COLUMN)));
			invHed.setFINVHED_TOTAL_AMT(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_TOTAL_AMT)));
			invHed.setFINVHED_TOTALDIS(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_TOTALDIS)));
			invHed.setFINVHED_TOTAL_TAX(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_TOTAL_TAX)));
			invHed.setFINVHED_TXN_TYPE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_TXN_TYPE)));
			invHed.setFINVHED_TXN_DATE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_TXN_DATE)));
			invHed.setFINVHED_ADDRESS(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_ADDRESS)));
			invHed.setFINVHED_TOTAL_ITM_DIS(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_TOTAL_ITM_DIS)));
			invHed.setFINVHED_TOT_MKR_AMT(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_TOT_MKR_AMT)));
			invHed.setFINVHED_IS_SYNCED(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_IS_SYNCED)));
			invHed.setFINVHED_IS_ACTIVE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_IS_ACTIVE)));
			invHed.setFINVHED_ROUTE_CODE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_ROUTE_CODE)));
			invHed.setFINVHED_HED_DIS_VAL(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_HED_DIS_VAL)));
			invHed.setFINVHED_HED_DIS_PER_VAL(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_HED_DIS_PER_VAL)));
			invHed.setFINVHED_PAYMENT_TYPE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_PAYMENT_TYPE)));
			
			invHed.setFINVHED_REFNO1(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_REFNO1)));
			
			
			list.add(invHed);
			
		}
		
		return list;
	}
	
	public ArrayList<VanSalesMapper> getAllUnsynced() {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<VanSalesMapper> list = new ArrayList<VanSalesMapper>();
		
		
		@SuppressWarnings("static-access")
		String selectQuery = "select * from "+dbHelper.TABLE_FINVHED
								+" Where "+dbHelper.FINVHED_IS_ACTIVE+"='0' and "+ dbHelper.FINVHED_IS_SYNCED+"='0'";
		
	
		Cursor cursor = dB.rawQuery(selectQuery, null);
		
		localSP = context.getSharedPreferences(SETTINGS,Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
		
		while(cursor.moveToNext()){
			
			VanSalesMapper vanSalesMapper =new VanSalesMapper();
		
			SalRepDS repDS=new SalRepDS(context);
			
			CompanyBranchDS branchDS=new CompanyBranchDS(context);
			vanSalesMapper.setNextNumVal(branchDS.getCurrentNextNumVal(context.getResources().getString(R.string.VanNumVal)));
						
			vanSalesMapper.setDistDB(localSP.getString("Dist_DB", "").toString());
			vanSalesMapper.setConsoleDB(localSP.getString("Console_DB", "").toString());
			vanSalesMapper.setSALEREP_DEALCODE(repDS.getDealCode());
			vanSalesMapper.setSALEREP_AREACODE(repDS.getAreaCode());
			
			
			vanSalesMapper.setFINVHED_ID(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_ID)));
			vanSalesMapper.setFINVHED_REFNO(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_REFNO)));
			vanSalesMapper.setFINVHED_ADD_DATE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_ADD_DATE)));
			vanSalesMapper.setFINVHED_ADD_MACH(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_ADD_MACH)));
			vanSalesMapper.setFINVHED_ADD_USER(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_ADD_USER)));
			vanSalesMapper.setFINVHED_APP_DATE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_APP_DATE)));
			vanSalesMapper.setFINVHED_APPSTS(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_APPSTS)));
			vanSalesMapper.setFINVHED_APP_USER(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_APP_USER)));
			vanSalesMapper.setFINVHED_BP_TOTAL_DIS(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_BP_TOTAL_DIS)));
			vanSalesMapper.setFINVHED_B_TOTAL_AMT(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_B_TOTAL_AMT)));
			vanSalesMapper.setFINVHED_B_TOTAL_DIS(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_B_TOTAL_DIS)));
			vanSalesMapper.setFINVHED_B_TOTAL_TAX(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_B_TOTAL_TAX)));
			vanSalesMapper.setFINVHED_COST_CODE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_COST_CODE)));
			vanSalesMapper.setFINVHED_CUR_CODE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_CUR_CODE)));
			vanSalesMapper.setFINVHED_CUR_RATE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_CUR_RATE)));
			vanSalesMapper.setFINVHED_DEB_CODE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_DEB_CODE)));
			vanSalesMapper.setFINVHED_DIS_PER(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_DIS_PER)));
			vanSalesMapper.setFINVHED_START_TIME_SO(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_START_TIME_SO)));
			vanSalesMapper.setFINVHED_END_TIME_SO(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_END_TIME_SO)));
			vanSalesMapper.setFINVHED_LONGITUDE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_LONGITUDE)));
			vanSalesMapper.setFINVHED_LATITUDE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_LATITUDE)));
			vanSalesMapper.setFINVHED_LOC_CODE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_LOC_CODE)));
			vanSalesMapper.setFINVHED_MANU_REF(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_MANU_REF)));
			vanSalesMapper.setFINVHED_RECORD_ID(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_RECORD_ID)));
			vanSalesMapper.setFINVHED_REMARKS(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_REMARKS)));
			vanSalesMapper.setFINVHED_REPCODE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_REPCODE)));
			vanSalesMapper.setFINVHED_TAX_REG(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_TAX_REG)));
			vanSalesMapper.setFINVHED_TIMESTAMP_COLUMN(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_TIMESTAMP_COLUMN)));
			vanSalesMapper.setFINVHED_TOTAL_AMT(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_TOTAL_AMT)));
			vanSalesMapper.setFINVHED_TOTALDIS(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_TOTALDIS)));
			vanSalesMapper.setFINVHED_TOTAL_TAX(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_TOTAL_TAX)));
			vanSalesMapper.setFINVHED_TXN_TYPE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_TXN_TYPE)));
			vanSalesMapper.setFINVHED_TXN_DATE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_TXN_DATE)));
			vanSalesMapper.setFINVHED_ADDRESS(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_ADDRESS)));
			vanSalesMapper.setFINVHED_TOTAL_ITM_DIS(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_TOTAL_ITM_DIS)));
			vanSalesMapper.setFINVHED_TOT_MKR_AMT(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_TOT_MKR_AMT)));
			vanSalesMapper.setFINVHED_IS_SYNCED(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_IS_SYNCED)));
			vanSalesMapper.setFINVHED_IS_ACTIVE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_IS_ACTIVE)));
			vanSalesMapper.setFINVHED_ROUTE_CODE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_ROUTE_CODE)));
			vanSalesMapper.setFINVHED_REFNO1(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_REFNO1)));
			vanSalesMapper.setFINVHED_HED_DIS_VAL(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_HED_DIS_VAL)));
			vanSalesMapper.setFINVHED_HED_DIS_PER_VAL(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_HED_DIS_PER_VAL)));
			vanSalesMapper.setFINVHED_PAYMENT_TYPE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_PAYMENT_TYPE)));
			
			vanSalesMapper.setInvDets(new InvDetDS(context).getAllInvDet(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_REFNO))));
			
			list.add(vanSalesMapper);
			
		}
		
		return list;
	}
	
	
	@SuppressWarnings("static-access")
	public int updateIsSynced(VanSalesMapper mapper) {
		
	int count =0;
		
	if(dB == null){
		open();
	}else if(!dB.isOpen()){
		open();
	}
	Cursor cursor = null;
		
	try{
		ContentValues values = new ContentValues();

		values.put(dbHelper.FINVHED_IS_SYNCED,"1");
		
		if(mapper.isIS_SYNCED()){
			count = dB.update(dbHelper.TABLE_FINVHED, values,dbHelper.FINVHED_REFNO + " =?",	
							new String[] { String.valueOf(mapper.getFINVHED_REFNO())});
		}
		
	}catch (Exception e) {
		
		Log.v(TAG+ " Exception", e.toString());
	

	}finally {  
		if (cursor !=null) {
			cursor.close();
			}
		dB.close();
		}
	return count;
				
}
	
	public int InactiveStatusUpdate(String refno) {
		
	int count =0;
		
	if(dB == null){
		open();
	}else if(!dB.isOpen()){
		open();
	}
	Cursor cursor = null;
		
	try{
					
		String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FINVHED 
				+ " WHERE " + dbHelper.FINVHED_REFNO + " = '" +refno + "'";	
			
		cursor = dB.rawQuery(selectQuery, null);
		
		ContentValues values = new ContentValues();
				
		values.put(dbHelper.FINVHED_IS_ACTIVE,"0");
			
		int cn = cursor.getCount();
			
		if(cn>0){
			count = dB.update(dbHelper.TABLE_FINVHED, values,dbHelper.FINVHED_REFNO + " =?",	
							new String[] { String.valueOf(refno)});
		}else{
			count = (int) dB.insert(dbHelper.TABLE_FINVHED, null, values);
		}
				
		
	}catch (Exception e) {
		
		Log.v(TAG+ " Exception", e.toString());
	
	}finally {  
		if (cursor !=null) {
			cursor.close();
			}
		dB.close();
		}
	return count;
				
	}
	
	@SuppressWarnings("static-access")
	public int restData(String refno) {
		
	int count =0;
		
	if(dB == null){
		open();
	}else if(!dB.isOpen()){
		open();
	}
	Cursor cursor = null;
		
	try{
					
		String selectQuery = "SELECT * FROM " + dbHelper.TABLE_FINVHED + " WHERE " + dbHelper.FINVHED_REFNO + " = '" +refno + "'";	
		cursor = dB.rawQuery(selectQuery, null);
		int cn = cursor.getCount();
			
		if(cn>0){
			int success = dB.delete(dbHelper.TABLE_FINVHED, dbHelper.FINVHED_REFNO + " ='"+refno+"'", null);
			Log.v("Success", success+"");
		}
				
		
	}catch (Exception e) {
		Log.v(TAG+ " Exception", e.toString());
	}
		finally {  
			if (cursor !=null) {
				cursor.close();
				}
			dB.close();
		}
	return count;
				
}
	
	public ArrayList<InvHed> getAllUnsyncedInvHed(String newText) {
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		ArrayList<InvHed> list = new ArrayList<InvHed>();
		Cursor cursor = null;
	try {
			
		@SuppressWarnings("static-access")
		String selectQuery = "select * from finvHed sa, fdebtor cu where sa.isActive='0' and sa.isSynced ='0' and sa.DebCode=cu.DebCode and sa.RefNo || sa.AddDate || cu.DebName  like '%"+newText+"%'";
		
	
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext()){
			
			InvHed invHed =new InvHed();
		
			invHed.setFINVHED_ID(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_ID)));
			invHed.setFINVHED_REFNO(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_REFNO)));
			invHed.setFINVHED_ADD_DATE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_ADD_DATE)));
			invHed.setFINVHED_ADD_MACH(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_ADD_MACH)));
			invHed.setFINVHED_ADD_USER(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_ADD_USER)));
			invHed.setFINVHED_APP_DATE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_APP_DATE)));
			invHed.setFINVHED_APPSTS(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_APPSTS)));
			invHed.setFINVHED_APP_USER(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_APP_USER)));
			invHed.setFINVHED_BP_TOTAL_DIS(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_BP_TOTAL_DIS)));
			invHed.setFINVHED_B_TOTAL_AMT(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_B_TOTAL_AMT)));
			invHed.setFINVHED_B_TOTAL_DIS(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_B_TOTAL_DIS)));
			invHed.setFINVHED_B_TOTAL_TAX(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_B_TOTAL_TAX)));
			invHed.setFINVHED_COST_CODE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_COST_CODE)));
			invHed.setFINVHED_CUR_CODE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_CUR_CODE)));
			invHed.setFINVHED_CUR_RATE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_CUR_RATE)));
			invHed.setFINVHED_DEB_CODE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_DEB_CODE)));
			invHed.setFINVHED_DIS_PER(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_DIS_PER)));
			invHed.setFINVHED_START_TIME_SO(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_START_TIME_SO)));
			invHed.setFINVHED_END_TIME_SO(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_END_TIME_SO)));
			invHed.setFINVHED_LONGITUDE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_LONGITUDE)));
			invHed.setFINVHED_LATITUDE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_LATITUDE)));
			invHed.setFINVHED_LOC_CODE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_LOC_CODE)));
			invHed.setFINVHED_MANU_REF(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_MANU_REF)));
			invHed.setFINVHED_RECORD_ID(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_RECORD_ID)));
			invHed.setFINVHED_REMARKS(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_REMARKS)));
			invHed.setFINVHED_REPCODE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_REPCODE)));
			invHed.setFINVHED_TAX_REG(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_TAX_REG)));
			invHed.setFINVHED_TIMESTAMP_COLUMN(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_TIMESTAMP_COLUMN)));
			invHed.setFINVHED_TOTAL_AMT(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_TOTAL_AMT)));
			invHed.setFINVHED_TOTALDIS(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_TOTALDIS)));
			invHed.setFINVHED_TOTAL_TAX(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_TOTAL_TAX)));
			invHed.setFINVHED_TXN_TYPE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_TXN_TYPE)));
			invHed.setFINVHED_TXN_DATE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_TXN_DATE)));
			invHed.setFINVHED_ADDRESS(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_ADDRESS)));
			invHed.setFINVHED_TOTAL_ITM_DIS(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_TOTAL_ITM_DIS)));
			invHed.setFINVHED_TOT_MKR_AMT(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_TOT_MKR_AMT)));
			invHed.setFINVHED_IS_SYNCED(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_IS_SYNCED)));
			invHed.setFINVHED_IS_ACTIVE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_IS_ACTIVE)));
			invHed.setFINVHED_ROUTE_CODE(cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_ROUTE_CODE)));
			
			Log.v("llooolllooollloolllooll", cursor.getString(cursor.getColumnIndex(dbHelper.FINVHED_ROUTE_CODE)));
			
			list.add(invHed);
			
		}
		
		
		
		}catch (Exception e) {
			// TODO: handle exception
			Log.v("Erorr :- ", e.toString());
		
		}finally{  
			if (cursor !=null) {
				cursor.close();
				}
			dB.close();
			}
	return list;
 }
	
}
